package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.MathUtil;
import com.xbongbong.paas.toolbox.util.RegexUtil;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.ProErrorCodeEnum;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Service("workflowOperatorNumHandlerImpl")
public class WorkflowOperatorNumHandlerImpl implements WorkflowOperatorFieldHandler {

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.NUM.getType());
    }


    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        // 默认工厂
        if (Objects.isNull(data.get(fieldAttrEntity.getAttr()))) {
            return 0;
        }
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo,
                                         WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        // 默认工厂
        if (Objects.isNull(workflowDataPojo.getValue())) {
            Object num = data.get(fieldAttrEntity.getAttr());
            if (Objects.isNull(num)) {
                return 0;
            }
            return num;
        }
        // 直接将当前表单的数据赋值给目标表单，无需进行精度计算；
        return workflowDataPojo.getValue();
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        Object object = workflowOperationsPojo.getValue();
        if (object instanceof String && RegexUtil.isNumber(object.toString())) {
            // 对字符串的数字或者浮点型进行强制转换
            if (object.toString().contains("\\.")) {
                return Double.valueOf(workflowOperationsPojo.getValue().toString());
            } else {
                return Long.valueOf(object.toString());
            }
        }
        // 如果是非数字类型，不进行加减
        if (!(object instanceof Number)) {
            throw new XbbException(ProErrorCodeEnum.API_ERROR_260017);
        }
        return object;
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data, Long formId, Integer businessType,  String corpid) throws XbbException {
        Object value = data.get(fieldAttrEntity.getAttr());
        if (Objects.isNull(value)) {
            value = 0;
        }
        value = MathUtil.scientificNotation(value);
        return value;
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        if (value instanceof String) {
            if (RegexUtil.isInteger(value.toString())) {
                long num = Long.valueOf(value.toString());
                if (num <= Integer.MAX_VALUE && num >= Integer.MIN_VALUE) {
                    value = (int) num;
                } else {
                    value = num;
                }
            } else {
                value = Double.valueOf(value.toString());
            }
        }
        Integer accuracy = targetField.getAccuracy();
        if (Objects.equals(1, targetField.getIntegerOnly())) {
            accuracy = 0;
        }
        return MathUtil.ceil(value, accuracy);
    }
}
